package com.huawei.statistics.service.impl.member;


import com.huawei.statistics.pojo.member.MemberType;
import com.huawei.statistics.pojo.member.User;
import com.huawei.statistics.service.impl.common.BaseServiceImpl;
import com.huawei.statistics.service.member.MemberService;
import com.huawei.statistics.service.member.MemberTypeService;
import com.huawei.statistics.vo.common.HttpResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018\4\10 0010.
 */

@Service
public class MemberTypeServiceImpl extends BaseServiceImpl implements MemberTypeService {


    @Autowired
    HttpSession httpSession;
    @Autowired
    MemberService memberService;




    @Override
    public MemberType getMemberTypeById(Long id) {

        return getEntityById(id,MemberType.class);
    }

    @Override
    public List<MemberType> getMemberTypeByCompanyId(Long companyId) {

        Map<String,Object> map = new HashMap<>();
        map.put("companyId",companyId);
        map.put("del",false);
        return getEntitiesBySort(MemberType.class,map,"id",true);
    }

    @Override
    public HttpResult addMemberTypeByCompanyId(Long companyId,String typeName,int min) {

        MemberType type = new MemberType();
        type.setCompanyId(companyId);
        type.setGroupName(typeName);
        type.setMinIntegral(min);
        type.setDel(false);
        return new HttpResult(HttpResult.OK,addEntity(type));
    }




    @Override
    public HttpResult deleteMemberTypeById(Long id) {
        if(id==1) return new HttpResult(HttpResult.OK,"普通会员组不能被删除");
        MemberType type = getEntityById(id,MemberType.class);
        Map<String,Object> map = new HashMap<>();
        map.put("memberGroup",type.getId());
        List<User> list = getEntityByMap(User.class,map);
        if(list == null && list.size() == 0) return new HttpResult(HttpResult.OK,"会员分钟没有会员");
        for(User user:list){
            user.setMemberGroup(0L);
            updateEntityValues(user);
        }
        type.setDel(true);
       return new HttpResult(HttpResult.OK,updateEntityValues(type));

    }



    @Override
    public HttpResult updateMemberTypeById(MemberType type) {
        type.setDel(false);
        return new HttpResult(HttpResult.OK,updateEntityValues(type));
    }
}
